﻿// This software is part of the Autofac IoC container
// Copyright © 2013 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#region 导入名称空间

using System;
using System.Diagnostics.CodeAnalysis;
using System.Security;
using Autofac.Core;
using LightWork.DynamicProxy;

#endregion

// ReSharper disable once CheckNamespace

namespace Autofac.Interceptor
{
    /// <summary>
    /// Autofac 扩展，标记一个接口或类应该被拦截
    /// </summary>
    [SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
    public sealed class InterceptAttribute : Attribute
    {
        /// <summary>
        /// 初始化<see cref="InterceptAttribute" />类的新实例
        /// </summary>
        /// <param name="interceptorService">拦截器服务</param>
        /// <exception cref="System.ArgumentNullException">interceptorService</exception>
        public InterceptAttribute(Service interceptorService)
        {
            if (interceptorService == null)
                throw new ArgumentNullException(nameof(interceptorService));

            InterceptorService = interceptorService;
        }

        /// <summary>
        /// 初始化<see cref="InterceptAttribute" />类的新实例
        /// </summary>
        /// <param name="interceptorServiceName">拦截器服务名</param>
        [SecuritySafeCritical]
        public InterceptAttribute(string interceptorServiceName)
            : this(new KeyedService(interceptorServiceName, typeof(IInterceptor)))
        {
        }

        /// <summary>
        /// 初始化<see cref="InterceptAttribute" />类的新实例
        /// </summary>
        /// <param name="interceptorServiceType">拦截器服务类型</param>
        public InterceptAttribute(Type interceptorServiceType)
            : this(new TypedService(interceptorServiceType))
        {
        }

        /// <summary>
        /// 获取拦截器服务
        /// </summary>
        public Service InterceptorService { get; }
    }
}